public class SolutionInterview1_6 {
    public String compressString(String S) {
        StringBuilder stringBuilder=new StringBuilder();
        for (int i=0;i<S.length();i++){
            int t=1;
            char c=S.charAt(i);
            while (i+1<S.length() && S.charAt(i+1)==c){
                t++;
                i++;
            }
            stringBuilder.append(c).append(t);
        }
        if (stringBuilder.length()>=S.length()){
            return S;
        }else {
            return stringBuilder.toString();
        }
    }

    public static void main(String[] args) {
        System.out.println(new SolutionInterview1_6().compressString("aabcccccaaa"));
    }
}
